package com.cnd.scm.common.utils;


import com.alibaba.fastjson.JSONObject;
import com.cnd.scm.common.web.HttpResult;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;


/**
* @author 李启联 E-mail:liql@gillion.com.cn
* @version 创建时间：2017年9月26日 上午10:12:35
* 类说明:
*/
public class HttpUtils {
    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 获取HttpServletRequest对象
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 输出信息到浏览器
     * @param response
     * @param msg
     * @throws IOException
     */
    public static void print(HttpServletResponse response, int code, String msg) throws IOException {
        response.setContentType("application/json; charset=utf-8");
        HttpResult result = HttpResult.error(code, msg);
        String json = JSONObject.toJSONString(result);
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();
    }
    /**
   * 发送HTTP请求
   *
   * @param url
   * @param propsMap 发送的参数
   */
//    public static HttpResponse httpPost(String url, Map<String, Object> propsMap) {
//        HttpResponse response = new HttpResponse();
//        String responseMsg = "";
//
//        HttpClient httpClient = new HttpClient();
//        PostMethod postMethod = new PostMethod(url);// POST请求
//        if (propsMap != null) {
//            // 参数设置
//            Set<String> keySet = propsMap.keySet();
//            NameValuePair[] postData = new NameValuePair[keySet.size()];
//            int index = 0;
//            for (String key : keySet) {
//                postData[index++] = new NameValuePair(key, propsMap.get(key)
//                        .toString());
//            }
//            postMethod.addParameters(postData);
//        }
//        postMethod.getParams().setParameter(
//                HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
//        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        try {
//            int statusCode = httpClient.executeMethod(postMethod);// 发送请求
//            response.setStatusCode(statusCode);
//            if (statusCode == HttpStatus.SC_OK) {
//                // 读取内容
//                byte[] responseBody = postMethod.getResponseBody();
//                // 处理返回的内容
//                responseMsg = new String(responseBody, "utf-8");
//            }
//        } catch (HttpException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            postMethod.releaseConnection();// 关闭连接
//        }
//        response.setContent(responseMsg);
//        return response;
//    }

    /**
     * 发送HTTP请求
     *
     * @param url
     */
//    public static HttpResponse httpGet(String url) {
//        HttpResponse response = new HttpResponse();
//        String responseMsg = "";
//
//        HttpClient httpClient = new HttpClient();
//        GetMethod getMethod = new GetMethod(url);
//
//        try {
//            int statusCode = httpClient.executeMethod(getMethod);// 发送请求
//            response.setStatusCode(statusCode);
//            if (statusCode == HttpStatus.SC_OK) {
//                // 读取内容
//                byte[] responseBody = getMethod.getResponseBody();
//                // 处理返回的内容
//                responseMsg = new String(responseBody, "utf-8");
//            }
//        } catch (HttpException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            getMethod.releaseConnection();// 关闭连接
//        }
//        response.setContent(responseMsg);
//        return response;
//    }
    
    /**
     * 发送HTTP--GET请求
     * 
     * @param url
     *            发送的参数
     */
    public static String httpGetSend(String url) {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);// GET请求
        try {
            // http超时5秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            // 设置 get 请求超时为 5 秒
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            httpClient.executeMethod(getMethod);// 发送请求
            // 读取内容
            byte[] responseBody = getMethod.getResponseBody();
            // 处理返回的内容
            responseMsg = new String(responseBody, "utf-8");
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();// 关闭连接
        }
        return responseMsg;
    }
    
    
    /**
     * 发送HTTP请求
     * 
     * @param url
     * @param propsMap
     *            发送的参数
     */
    public static String httpSend(String url, Map<String, Object> propsMap) {
        String responseMsg = "";

        HttpClient httpClient = new HttpClient();
        
        PostMethod postMethod = new PostMethod(url);// POST请求

//        postMethod.
        // 参数设置
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        try {
            httpClient.executeMethod(postMethod);// 发送请求
//            Log.info(postMethod.getStatusCode());
            // 读取内容
            byte[] responseBody = postMethod.getResponseBody();
            // 处理返回的内容
            responseMsg = new String(responseBody);

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();// 关闭连接
        }
        return responseMsg;
    }
    
    
    /**
     * 发送Post HTTP请求
     * 
     * @param url
     * @param propsMap
     *            发送的参数
     * @throws IOException 
     * @throws HttpException 
     */
    public static PostMethod httpSendPost(String url, Map<String, Object> propsMap,String authrition) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST请求
        postMethod.addRequestHeader("Authorization",authrition);
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 参数设置
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        
        httpClient.executeMethod(postMethod);// 发送请求
        return postMethod;
    }
    
    /**
     * 发送Post HTTP请求
     * 
     * @param url
     * @param propsMap
     *            发送的参数
     * @throws IOException 
     * @throws HttpException 
     */
    public static PostMethod httpSendPost(String url, Map<String, Object> propsMap) throws Exception {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST请求
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 参数设置
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key)
                    .toString());
        }
        postMethod.addParameters(postData);
        
        httpClient.executeMethod(postMethod);// 发送请求
        return postMethod;
    }
    
    
    /**
     * 发送Get HTTP请求
     * 
     * @param url
     * @param propsMap
     *            发送的参数
     * @throws IOException 
     * @throws HttpException 
     */
    public static GetMethod httpSendGet(String url, Map<String, Object> propsMap) throws Exception {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);// GET请求
        getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
        // 参数设置
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            getMethod.getParams().setParameter(key, propsMap.get(key)
                    .toString());
        }
        
        httpClient.executeMethod(getMethod);// 发送请求
        return getMethod;
    }
}
